﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ParkingYunService
{
    public static class LogManage
    {
        public static object LockObject = new object();
        public static bool Processing = false;
        /// <summary>
        /// 日志队列，防止多线程写文件造成的IO错误 每一分钟处理一次
        /// </summary>
        public static List<LogMsg> listLog = new List<LogMsg>();
        /// <summary>
        /// 接收数据记录
        /// </summary>
        public static List<Data> ReceiveDataStr = new List<Data>();
        /// <summary>
        /// 接收数据记录
        /// </summary>
        public static List<Data> SendDataStr = new List<Data>();
        /// <summary>
        /// 默认为异常信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="strMsg"></param>
        public static void WriteLog(string title, string strMsg)
        {

            listLog.Add(new LogMsg(title, strMsg));
        }
        /// <summary>
        /// ServerSocket异常
        /// </summary>
        /// <param name="strMsg"></param>
        public static void WriteLog(string strMsg)
        {
            listLog.Add(new LogMsg("ServerSocket异常", strMsg));
        }
        public static void WriteLog()
        {
            string title = "", strMsg = "";

            for (int i = 0; listLog.Count > 0; )
            {
                DateTime dtNow = DateTime.Now;
                title = listLog[i].Title;
                strMsg = listLog[i].Content;
                string R_startPath = Application.StartupPath;

                string strFileName = dtNow.ToString("yyyyMMdd") + "_log.txt";
                string filePath = R_startPath + "\\Logs\\" + strFileName;
                if (!Directory.Exists(R_startPath + "\\Logs"))
                {
                    Directory.CreateDirectory(R_startPath + "\\Logs");
                }
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }

                try
                {
                    using (StreamWriter sw = File.AppendText(filePath))
                    {

                        sw.WriteLine("\r\n/*-----" + title + "  " + listLog[i].DateNow.ToString("yyyy-MM-dd HH:mm:ss") + "-------*/");
                        sw.WriteLine(strMsg + "\r\n");
                        sw.Close();


                    }
                }
                catch (Exception)
                {
                    Processing = false;

                }
                finally
                {
                    listLog.Remove(listLog[i]);
                }
            }

        }
        /// <summary>
        /// 写入接收数据
        /// </summary>
        /// <param name="str"></param>
        public static void WriteReceive(string str)
        {
            ReceiveDataStr.Add(new Data { date = str, DateNow = DateTime.Now });
        }
        /// <summary>
        /// 写入接收的数据
        /// </summary>
        public static void WriteReceive()
        {

            for (int i = 0; ReceiveDataStr.Count > 0; )
            {
                DateTime dtNow = DateTime.Now;
                string R_startPath = Application.StartupPath;

                string strFileName = dtNow.ToString("yyyyMMdd") + ".txt";
                string filePath = R_startPath + "\\ReceiveData\\" + strFileName;
                if (!Directory.Exists(R_startPath + "\\ReceiveData"))
                {
                    Directory.CreateDirectory(R_startPath + "\\ReceiveData");
                }
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }

                try
                {
                    using (StreamWriter sw = File.AppendText(filePath))
                    {

                        sw.WriteLine("\r\n/*------- " + ReceiveDataStr[i].DateNow.ToString("yyyy-MM-dd HH:mm:ss") + "-------*/");
                        sw.WriteLine(ReceiveDataStr[i].date + "\r\n");
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    Processing = false;

                }
                finally
                {
                    ReceiveDataStr.Remove(ReceiveDataStr[i]);
                }
            }

        }
        /// <summary>
        /// 写入发送数据
        /// </summary>
        /// <param name="str"></param>
        public static void WriteSend(string str)
        {
            SendDataStr.Add(new Data { date=str,DateNow=DateTime.Now});
        }
        /// <summary>
        /// 写入发出的数据
        /// </summary>
        public static void WriteSend()
        {

            for (int i = 0; SendDataStr.Count > 0; )
            {
                DateTime dtNow = DateTime.Now;
                string R_startPath = Application.StartupPath;

                string strFileName = dtNow.ToString("yyyyMMdd") + ".txt";
                string filePath = R_startPath + "\\SendData\\" + strFileName;
                if (!Directory.Exists(R_startPath + "\\SendData"))
                {
                    Directory.CreateDirectory(R_startPath + "\\SendData");
                }
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }

                try
                {
                    using (StreamWriter sw = File.AppendText(filePath))
                    {

                        sw.WriteLine("\r\n/*----- " + SendDataStr[i].DateNow.ToString("yyyy-MM-dd HH:mm:ss") + "-------*/");
                        sw.WriteLine(SendDataStr[i].date + "\r\n");
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    Processing = false;

                }
                finally
                {
                    SendDataStr.Remove(SendDataStr[i]);
                }
            }

        }
    }
    /// <summary>
    /// 操作日志:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public class LogMsg
    {
        public LogMsg(string _Title, string _Content)
        {
            this._Content = _Content;
            this._Title = _Title;
            this._DateNow = DateTime.Now;
        }
        #region Model


        private string _Title;

        private string _Content;



        /// <summary>
        /// 
        /// </summary>
        public string Title
        {
            set { _Title = value; }
            get { return _Title; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Content
        {
            set { _Content = value; }
            get { return _Content; }
        }

        private DateTime _DateNow;

        /// <summary>
        /// 
        /// </summary>
        public DateTime DateNow
        {
            set { _DateNow = value; }
            get { return _DateNow; }
        }

        #endregion Model
    }
    /// <summary>
    /// 数据实体
    /// </summary>
    [Serializable]
    public class Data 
    {
        public string date { get; set; }
        public DateTime DateNow { get; set; }
    }
}
